package com.finance.model.ip;

import com.alibaba.fastjson.JSONObject;
import com.finance.model.ip.dto.IpRequestDto;
import com.finance.model.ip.dto.IpResponseDto;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author: Apollo
 * @Date: 2021/4/18
 * @Description: ip查询
 */
@Service
public class Ip {

    @Resource
    private OkHttpClient okHttpClient;


    /**
     * Ip地址查询
     * @throws IOException
     */
    public IpResponseDto addressInquiries() throws IOException {
        //OkHttpClient okHttpClient = new OkHttpClient();
        //请求构造器
        Request.Builder builder = new Request.Builder();
        IpRequestDto ipRequestDto = new IpRequestDto();
        ipRequestDto.setIp("localhost");
        //构造路径
        builder.url(IpRequestDto.SERVICE_URL+"?"+ IpRequestDto.COL_IP+"="+ipRequestDto.getIp());
        builder.get();
        //构造头部内容
        builder.header(IpRequestDto.AUTHORIZATION_HEADER_KEY, IpRequestDto.APP_CODE_PRE+ IpRequestDto.APP_CODE);
        Request request = builder.build();
        //客户端执行
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        String jsonString = response.body().string();
        //处理json字符串
        IpResponseDto ipResponseDto = JSONObject.parseObject(jsonString, IpResponseDto.class);
        System.out.println(ipResponseDto);
        return ipResponseDto;
    }


//    public static void main(String[] args) throws IOException {
//        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
//        annotationConfigApplicationContext.scan("org.wyk.dto");
//        //Ip地址查询
//        addressInquiries();
//    }

}
